package com.ehotting.edsta.mqbd.monitor.web.controller;

import com.ehotting.mqbd.api.bean.solver.SolverCountBean;
import com.ehotting.mqbd.api.enums.DirectionEnum;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.mqbd.api.bean.MqApplicationContext;
import com.ehotting.mqbd.core.helper.IPHelper;
import com.ehotting.mqbd.core.helper.MQStringHelper;
import com.ehotting.mqbd.core.helper.RocketMQAdminHelper;
import com.ehotting.mqbd.core.manager.SolverCountManager;
import com.ehotting.mqbd.core.manager.SolverStateManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/monitor")
@Slf4j
public class MonitorController {

    @Autowired
    private SolverStateManager solverStateManager;
    @Autowired
    private RocketMQAdminHelper rocketMQAdminHelper;
    @Autowired
    private IPHelper ipHelper;
    @Autowired
    private SolverCountManager solverCountManager;

    @GetMapping("/state")
    public Object solverInfo(String module){
        return  solverStateManager.findByModule(module);
    }

    @GetMapping("/release")
    public Object releaseServer(String module,String ip){
        solverStateManager.release(module,ip);
        return "OK";
    }

    @GetMapping("/live")
    public Object liveServer(String module,String ip){
        solverStateManager.live(module,ip);
        return "OK";
    }

    @GetMapping("/countQueue")
    public Object count(String module,String ip){
        MqApplicationContext mqApplicationContext = new MqApplicationContext();
        mqApplicationContext.setLocalHost(ip);
        mqApplicationContext.setModule(module);
        return  rocketMQAdminHelper.getAllQueueSize(
                MQStringHelper.createTopic(TopicEnum.BLOCK,mqApplicationContext.getModule(), DirectionEnum.REQUEST),
                MQStringHelper.createConsumerGroupAtConsumerApp(TopicEnum.BLOCK, mqApplicationContext , DirectionEnum.REQUEST));
    }

    @GetMapping("/all")
    public Object countAll(){
        List<SolverCountBean> serverCountList = solverCountManager.getCountAll();
        return serverCountList;
    }

    @GetMapping("/ip")
    public Object ip(){
        return ipHelper.getIp();
    }

    @GetMapping("/ipPreList")
    public Object ipPreList(){
        return ipHelper.getIpPreList();
    }

}